from selenium import webdriver
from time import sleep
import csv


def writecsv(item):
    with open('黄河口11.26.csv', 'a', encoding='utf-8', newline='')as f:
        writer = csv.writer(f)
        try:
            writer.writerow(item)
        except:
            print("write error")


def getElement():
    # w = 200
    driver = webdriver.Chrome(r'E:\Google\chromedriver.exe')
    for i in range(1, 23):
        driver.get('https://www.hhk365.com/zhaopin/#mywin')
        sleep(2)
        # driver.execute_script('window.scrollTo(0,'+str(w)+');')
        # w = w+50

        try:
            driver.find_element_by_xpath('/html/body/div[5]/div/ul[3]/li[' + str(i) + ']/a').click()
            """/html/body/div[6]/div[1]/div[2]/ul[1]/li[1]/a"""
            """/html/body/div[6]/div[1]/div[2]/ul[35]/li[1]/a"""
            """/html/body/div[5]/div/ul[2]/li[1]/a"""
            """/html/body/div[5]/div/ul[1]/li[5]/a"""
            windows = driver.window_handles
            driver.switch_to.window(windows[-1])
            a = driver.find_element_by_xpath("/html/body/div[5]/div[1]/div[1]/div[1]/strong")  # 工作名称
            time = driver.find_element_by_xpath("/html/body/div[5]/div[1]/div[1]/div[2]/div[1]")  # 时间
            time_text = time.text
            time_list = time_text.split('：')
            times = time_list[1]
            times_title = times.split(' ')
            rec = times_title[0]

            b2 = driver.find_element_by_xpath("/html/body/div[5]/div[1]/div[1]/div[3]/dl[1]/dd/a")  # 工种
            b3 = driver.find_element_by_xpath('//*[@id="xxjsreplace"]')  # 信息描述
            b4 = driver.find_element_by_xpath("/html/body/div[5]/div[1]/div[1]/div[8]/p[1]/span/a")  # 联系人
            b5 = driver.find_element_by_xpath("/html/body/div[5]/div[1]/div[1]/div[8]/p[2]/img").get_attribute(
                "src")  # 电话
            province = ('山东省')
            city = ("东营市")
            b1 = driver.find_element_by_xpath("/html/body/div[5]/div[1]/div[1]/div[3]/dl[3]/dd")  # 地址
            # b1_text = b1.text
            # b1_list = b1_text.split("-")
            # address = b1_list[1]

            lists = [a.text, b2.text, b3.text, rec, b4.text, b5, province, city, b1.text]
            print(lists)
            # writecsv(lists)
        except:
            print("NoSuchElementException")


# def recognition():
#     pytesseract.pytesseract.tesseract_cmd = 'D:/soft/recognition/Tesseract-OCR/tesseract.exe'
#     text = pytesseract.image_to_string(Image.open('E://figures/other/poems.jpg'))


if __name__ == "__main__":
    getElement()
